package com.efesco.crm.quotation;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;
import java.util.Vector;
import com.efesco.common.*;
import com.efesco.system.ClientRightControl;

/**
 * quotationTemplateAddViewModel
 */
public class quotationTemplateAddViewModel extends DefaultViewModel {
  protected void initDatasets()throws Exception {
    super.initDatasets();
    // Add your code here
  }

  protected void initControls() throws Exception {
    super.initControls();
    Control control=this.getControl("QUOTATION_TEMPLATE_EDIT");
    if(control!=null){
	    ClientRightControl clientRightControl=new ClientRightControl();
	    clientRightControl.rightControl("QUOTATION_TEMPLATE_EDIT", control,DoradoContext.getContext());
    }// Add your code here
  }

  protected void doLoadData() throws Exception {
    // Add your code here
    super.doLoadData();
  }

  public void add(ParameterSet parameters)throws Exception {
    // Add your code here
	Dataset dsTemplate=this.getDataset("datasetQuotationTemplate");
	String id=DBTools.getNewId("general");
	dsTemplate.setString("QUOTATION_TEMPLATE_ID",id);
	Dataset theDsItems=this.getDataset("datasetItems");  
	Dataset dsItems=theDsItems;
	//System.out.println("************************************************"+id);
    super.doUpdateData(parameters); 
    
    DBStatement statement=new DBStatement();
	String sql="";
	
    //Dataset dsItems=this.getDataset("datasetItems");
    dsItems.moveFirst();
    sql="delete from s_quotation_template_item where quotation_template_id="+id;
    statement.setSql(sql);
	statement.execute();
	statement.close();
    while(!dsItems.isLast()){
    	String iId=DBTools.getNewId("general");
    	sql="insert into s_quotation_template_item(quotation_template_item_id,"
    	   +"product_id,quotation_template_id)values("
    	   +""+iId+","+dsItems.getString("PRODUCT_ID")+","+id+")";
    	statement.setSql(sql);
    	statement.execute();
    	statement.close();
    	dsItems.moveNext();
    }
  }
  
  public void modify(ParameterSet parameters)throws Exception{
	    Dataset dsTemplate=this.getDataset("datasetQuotationTemplate");
		String id=dsTemplate.getString("QUOTATION_TEMPLATE_ID"); 
	    
	    DBStatement statement=new DBStatement();
		String sql="";
		
	    Dataset dsItems=this.getDataset("datasetItems");
	    dsItems.moveFirst();
	    
	    while(!dsItems.isLast()){
	    	if(dsItems.getString("select").equals("true")){
		    	dsItems.setValue("QUOTATION_TEMPLATE_ID",id);
		    	dsItems.setValue("QUOTATION_TEMPLATE_ITEM_ID",DBTools.getNewId("general"));
	    	}else{
	    		sql="delete from s_quotation_template_item where product_id="+dsItems.getString("PRODUCT_ID");
	    	    statement.setSql(sql);
	    		statement.execute();
	    		statement.close();
	    	}
	    	
	    	dsItems.moveNext();
	    }
	    
	    super.doUpdateData(parameters); 
  }
  public void delete(ParameterSet parameters)throws Exception{
	  Dataset dsTemplate=this.getDataset("datasetQuotationTemplate");
	  dsTemplate.setValue("IS_ENABLED","0");
	  super.doUpdateData(parameters); 
  }
 

}
